<html>
<head>
<title>upload test</title>
<script>
var globallog = ""
var pagelog;
function synclog()
{
    if(!pagelog)
		pagelog = document.getElementById("pagelog");
    pagelog.value = globallog;
	pagelog.scrollTop = pagelog.scrollHeight;
}

function writelog(text,sync)
{
    if(globallog.length > 32768)
        globallog = globallog.substring(16384);
    globallog += ""+text+"\n";
	if(sync)synclog();
}

Object.keys = Object.keys || (function () {
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !{toString:null}.propertyIsEnumerable("toString"),
        DontEnums = [ 
            'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty',
            'isPrototypeOf', 'propertyIsEnumerable', 'constructor'
        ],
        DontEnumsLength = DontEnums.length;
        
    return function (o) {
        if (typeof o != "object" && typeof o != "function" || o === null)
            throw new TypeError("Object.keys called on a non-object");
    
        var result = [];
        for (var name in o) {
            if (hasOwnProperty.call(o, name))
                result.push(name);
        }
    
        if (hasDontEnumBug) {
            for (var i = 0; i < DontEnumsLength; i++) {
                if (hasOwnProperty.call(o, DontEnums[i]))
                    result.push(DontEnums[i]);
            }   
        }
    
        return result;
    };
})();


function writeerror(e)
{
	var text = "Exception: "
	try{
		text += "" + e;
	}catch(e){}
	try{
		text += " " + e.name;
	}catch(e){}
	try{
		text += " " + e.message;
	}catch(e){}
	try{
		if(e.description)
			text += " " + e.description;
	}catch(e){}
	try{
		if(e.method)
			text += " " + e.method;
	}catch(e){}
	try{
		text += " " + e.fileName + ":" + (e.lineNumber || e.number) ;
	}catch(e){}
	try{
		if(e.stack)
			text += " " + e.stack;
	}catch(e){}
	// hack: rethrow error for onerror and browser-internal handler
	try{
		setTimeout(function(){throw(e);},100);
		writelog(Object.keys(e), true);
	}catch(e){}
	writelog(text, true);
}
</script>
</head>
<body onerror="writelog(event, true);writelog(event.message, true);">
<style>

html, body {
  height: 100%; }

html {
  background-color: #222; }

body {
  position: relative;
  /*max-width: 1024px;
  min-width: 768px;*/
  min-width: 600px;
  margin: 0 auto;
  font: 12px sans-serif;
}

header {
  color: #fff;
  background-color: #000;
  height: 24px;
  overflow: hidden; }

.sidebar td {
  color: #fff;
  background-color: #000;
 }
.sidebar a:link {
color: #FFA;
}
.sidebar a:visited {
color: #FFC;
}


.sidebar, .main {

  -webkit-overflow-scrolling: touch;
  position: absolute;
  	top: 24px;
  bottom: 0; }

.sidebar {
  overflow:scroll;
  overflow-x:auto;
  width: 50%;
  background-color: #333;
  left: 0; }

.main {
  overflow: hidden;
  background-color: #f5f5f5;
  position: absolute;
  left: 50%;
  width: 50%;
  right: 0;
  /*padding: 20px;*/
  padding-bottom:25px; }

</style>
<header><div style="position:absolute;overflow:hidden;color:white" id="pathdisplay"></div><div style="position:absolute;right:0px;overflow:hidden">
<input type="checkbox" id="enable_fileapi"/><label for="enable_fileapi" style="color:white;">Enable FileList API</label>
<input type="checkbox" id="enable_xhr"/><label for="enable_xhr"  style="color:white;">Enable XHR</label>
<button id="mkdir" onclick="mkdirClick()">Create Directory</button>
<button id="mkfile"  onclick="createFile();">Create File</button>

</div>
</header>
<div class="sidebar"><div id="filelist"></div><iframe src="/indexredir" width="100%" height="100%" style="border:0;padding:0;" loading="lazy" id="legacyframe"></iframe></div>
<div class="main" id="uploadcontainer">	
<table border="0" height="100%">
<tr><td><form><label for="filesdir">Upload&nbsp;dir:</label><br />
<input id="filesdir" name=filesdir
      type="file"
      directory="directory"
      multiple
	  size="4"
      webkitdirectory="webkitdirectory"  style="font:40% sans-serif;max-width:100px" disabled />
<input type="button" value="Upload" disabled id="upload" onclick="startUpload()" /></form></td><td  style="vertical-align: top;">
<form  id="legacyupload" method="POST" action="/legacyupload" encoding="multipart/form-data" enctype="multipart/form-data"><label for="file">Upload&nbsp;file:</label><br />
<input id="file" name="file"
      type="file" 
      multiple size="4" style="font:40% sans-serif;max-width:100px" />
<input type="submit" value="Send" id="uploadlegacy"/></form></td>
<td  style="vertical-align: top;"><form  id="zipform" method="POST" action="/legacyzip" encoding="multipart/form-data" enctype="multipart/form-data"><label for="zipfile">Upload&nbsp;zip:</label><br />
<input id="zipfile" name="zipfile"
      type="file"
 size="4" style="font:40% sans-serif;max-width:100px" />
<input type="submit" value="Extract" id="uploadzip"/></form></td>
<td border="0" width="100%" cellspacing="0"></td><td border="0" width="0" cellspacing="0"></td></tr><tr>
<td height="100%" width="100%" colspan="4">
<div id="dropcontainer" style="border:1px solid black;width:100%;height:75%;">Drop here</div><textarea id="pagelog"  style="width:100%;height:25%;resize:none;"></textarea>
</div>
</td><td border="0" cellspacing="0" style="width:0;border-color:#f5f5f5;border-width:0;padding:0;"><div style="height:200px;width:0;overflow:hidden;padding:0;" ><br/></div></td>
</tr></table></div>
<div id="editorcontainer" class="main" style="padding-bottom:40px;overflow:hidden;display:none">
<button onclick="saveEdit();">Save</button><button onclick="cancelEdit();">Cancel</button><br/>
<textarea id="editor" style="width:100%;height:100%;resize:none;"></textarea>
</div>	
</div>
<!--[if IE]>
<script>
window.onresize = document.body.onload =  function()
{
	uploadContainer.style.height = document.body.clientHeight - 26;
	if(oldFileList)
		oldFileList.style.height = document.body.clientHeight - 26;
	fileListContainer.parentNode.style.height = document.body.clientHeight - 26;
}
</script>
<![endif]-->

<script>
window.onerror = function(msg, url, lno) {
	
	writelog("Error handler:" + "\n" +
		"Error: " + msg + "\n" + "URL: " + url + "\n" +
		"Line Number: " + lno, true);
	
	return false; // let browser print error to
}
</script>


<script>

// ie

function parseJSON(text)
{
	if("JSON" in window)
	{
		return JSON.parse(text);
	}
	else return eval("("+text+")");
}

if(('navigator' in window) && window.navigator && window.navigator.userAgent.indexOf("ELinks") >= 0)
{
	writelog = alert;
	XMLHttpRequest.prototype.open_orig = XMLHttpRequest.prototype.open;
	XMLHttpRequest.prototype.open = function(a,b,c)
	{
		this.open_orig(a,document.location.href + b.substring(1),c);
	};
}

function getXMLHttpRequest()
{
	var xhr = null;

	if(window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else if(window.ActiveXObject) { // poor alternative, but at least filelist and editing works
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
			xhr.open("GET","/", true); // it may throw DLLError
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			writeerror(e);
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				writeerror(e);
			}
		}
	}
	else
	{
		writelog("Your browser doesn't support XMLHTTPRequest...", true);
	}
	return xhr;
}

var XMLHttpRequest_DONE = 4


var dropContainer = document.getElementById("dropcontainer")
dropContainer.ondragover = dropContainer.ondragenter = function(event) {
	if(!event) event = window.event;
	if(!event.target)
		event.target = event.srcElement;
	if(event && event.preventDefault)
		event.preventDefault();
	if(event.dataTransfer)
		event.dataTransfer.dropEffect = 'copy';
	return false;
};
dropContainer.ondrop = function(event) {
	if(!event) event = window.event;
	if(!event.target)
		event.target = event.srcElement;
	if(event && event.preventDefault)
		event.preventDefault();
 	if('Promise' in window && event.dataTransfer && event.dataTransfer.items && event.dataTransfer.items[0].webkitGetAsEntry)
	{
		writelog("Drop event, using async mode", true);
		processDTItemsAsync(event.dataTransfer.items).then(function(files){
			processFiles(files);
		});
	}
	else
	{
		writelog("Drop event, using sync mode (slow)", true);
		processDTItems(event.dataTransfer);
	}
	return false;
};

function resolveFile(resolve, item, path, fileList)
{
	item.file(function(file) {
		file.filepath = path + file.name; //save full path
		fileList.push(file);
		resolve(file);
	});
}

function resolveDir(resolve, item, path, fileList)
{
	var dirReader = item.createReader();
	dirReader.readEntries(function(entries){
		var entriesPromises = [];
		for (var i = 0; i < entries.length;i++)
			entriesPromises.push(traverseItems(entries[i], (path ? path :"") + item.name + "/", fileList));
		resolve(Promise.all(entriesPromises));
	});
}

function resolveItem(resolve, item, path, fileList)
{
	if (item.isFile)
		resolveFile(resolve, item, path, fileList);
	else if (item.isDirectory)
		resolveDir(resolve, item, path, fileList);
}

function traverseItems(item, path, fileList)
{
	return new Promise(function(resolve){
		resolveItem(resolve, item, path, fileList);
	});
}

function processDTItemsAsync(dataTransferItems) // do we ever need this???
{
	var files = [];
	return new Promise(function(resolve, reject){
	var entriesPromises = [];
		for (var i = 0; i < dataTransferItems.length;i++)
			entriesPromises.push(traverseItems(dataTransferItems[i].webkitGetAsEntry(), "", files));
		Promise.all(entriesPromises).then(function(entries){
			resolve(files);
		});
	});
}
function traverseFileTree(item, path) {
  path = path || "";
  if (item.isFile) {
    // Get file
    item.file(function(file) {
      file.filepath = path + file.name;
		selectedfiles.push(file);
		processFiles([]);
    });
  } else if (item.isDirectory) {
    // Get folder contents
    var dirReader = item.createReader();
    dirReader.readEntries(function(entries) {
      for (var i=0; i<entries.length; i++) {
        traverseFileTree(entries[i], path + item.name + "/");
      }
    });
  }
}
function processDTItems(dataTransfer)
{
	var items = dataTransfer.items;

	if(!items || !items[0].webkitGetAsEntry)
	{
		// this cannot handle folders at all
		writelog("no DataTransder items, folders will fail...", true);
		processFiles(dataTransfer.files);
		return;
	}

	for (var i=0; i < items.length; i++) {
		// this handles folders and files in async way, we'll update file list on every file (slow)
		var item = dataTransfer.items[i].webkitGetAsEntry();
		traverseFileTree(item);
	}
}

var pendingfiles = [];
var selectedfiles = [];
var uploadingfiles = [];

var list_path = "";

function uploadFile(file)
{
	uploadingfiles.push(file);
	var req = getXMLHttpRequest();
	var path = (file.filepath|| file.webkitRelativePath || file.name);
	writelog("uploading:"+path);
	req.open("PUT", "/files/"+list_path+(list_path.length >0?"/":"")+path, true);
	req.onreadystatechange = function(){
		//writelog("readyState("+path+"):"+req.readyState);
		if (req.readyState === XMLHttpRequest_DONE) {
			var status = req.status;
			//writelog("status("+path+"):"+status);


			if ((status === 0 || (status >= 200 && status < 400)) && req.responseText == 'OK' ) {

			} else {
				pendingfiles.push(file);
				writelog("error:"+path, true);
			}
 			var index = uploadingfiles.indexOf(file);
			if(index >= 0)
			uploadingfiles.splice(index, 1);
			scheduleUpload();
		}
	}
	req.send(file)
}
var last_update = new Date();
function updateStatus()
{
	var now = new Date();
	if(now - last_update > 400)
	{
		last_update = now;
		var count = pendingfiles.length;
		var status = "Files left: " + count + "\nUploading now:\n" ;
		for(var i = 0; i < uploadingfiles.length; i++)
		{
			var file = uploadingfiles[i];
			status += (file.filepath|| file.webkitRelativePath|| file.name) + "\n";
		}
		dropContainer.innerText = status;
		synclog();
	}
}



function scheduleUpload()
{
	while(uploadingfiles.length < 16 && pendingfiles.length > 0)
	{
		uploadFile(pendingfiles.pop())
	}
	updateStatus();
	if(uploadingfiles.length == 0 && pendingfiles.length == 0)
	{
		alert("upload done!");
		resetSelection();
		updateList();
	}
}

function processFiles(files)
{
	if(pendingfiles.length)
		return;	
	if(selectedfiles.length)
		selectedfiles = Array.prototype.concat(selectedfiles,Array.prototype.slice.call(files))
	else if(files)
		selectedfiles = Array.prototype.slice.call(files);

	var len = selectedfiles.length;
	uploadButton.disabled = false;
	dropContainer.innerText = "" + len + " files selected, click \"Upload\" to start now!";
}

function startUpload()
{
	pendingfiles = selectedfiles;
	uploadButton.disabled = true;
	if(oldFileList)
		fileListContainer.removeChild(oldFileList);
	oldFileList = null;
	scheduleUpload();
}

function resetSelection()
{
	dropContainer.innerText = "Drop here";
	selectedFiles = [];
}

function delfile(path)
{
	if(!confirm("Delete "+path))
		return;
	var req = getXMLHttpRequest();
	req.open("DELETE", path, true);
	req.onreadystatechange = function(){
		//writelog("readyState("+path+"):"+req.readyState);
		if (req.readyState === XMLHttpRequest.DONE) {
			updateList();
		}
	}
	req.send();
}
function folderWalk(name)
{
	if(list_path.length)
		list_path += '/';
	list_path += name;
	updateList();
}
function folderUp()
{
	var i = list_path.lastIndexOf('/');
	writelog(i, true);
	list_path = i >= 0?list_path.substring(0,i):"";
	updateList();
}

var oldFileList = null;
var fileListContainer = document.getElementById("filelist");
var pathDisplay = document.getElementById("pathdisplay");
var uploadButton = document.getElementById("upload");
var editorContainer = document.getElementById("editorcontainer");
var uploadContainer = document.getElementById("uploadcontainer");


function folderClick(event)
{
	if(!event) event = window.event;
	if(!event.target)
		event.target = event.srcElement;
	folderWalk(event.target.data);
}

function delClick(event)
{
	if(!event) event = window.event;
	if(!event.target)
		event.target = event.srcElement;
	delfile(event.target.data || event.srcElement.data);
}
var editpath = "";
function saveEdit()
{
	var text = document.getElementById("editor").value;
	writelog("Saving file: " + editpath, true);
	var req = getXMLHttpRequest();
	req.open("PUT", editpath, true);
	req.onreadystatechange = function(){
		writelog("readyState("+editpath+"):"+req.readyState);
		if (req.readyState === XMLHttpRequest_DONE) {
			var status = req.status;
			//writelog("status("+path+"):"+status);


			if ((status === 0 || (status >= 200 && status < 400))) {
				editorContainer.style.display = 'none';
				uploadContainer.style.display = 'block';
				updateList();
			}
		}
		synclog();
	};
	if('Blob' in window)
		req.send(new Blob([text],{type:"text/plain"}));
	else
		req.send(text);
}

function editFile(path)
{
	editpath = path;
	writelog("Editing file: " + editpath, true);
	var req = getXMLHttpRequest();
	req.open("GET", editpath, true);
	editorContainer.style.display = 'block';
	uploadContainer.style.display = 'none';

	req.onreadystatechange = function(){
		writelog("edreadyState("+editpath+"):"+req.readyState);
		if (req.readyState == XMLHttpRequest_DONE) {
				var status = req.status;
			writelog("edstatus():"+status);


			if ((status === 0 || (status >= 200 && status < 400)) && req.responseText.length >= 0 && req.responseText.length < 65535) {
				document.getElementById("editor").value = req.responseText;
			}
		}
		synclog();
	};
	req.send("");
}

function editClick(event)
{
	if(!event) event = window.event;
	if(!event.target)
		event.target = event.srcElement;

	editFile(event.target.data);
}

function createFile()
{
	editpath ="/files/"+list_path+(list_path.length >0?"/":"")+prompt("New file name", "");
	editorContainer.style.display = 'block';
	uploadContainer.style.display = 'none';
	document.getElementById("editor").value = "";
}

function cancelEdit()
{
	editorContainer.style.display = 'none';
	uploadContainer.style.display = 'block';
}

function delDirClick(event)
{
}

function zipClick(event)
{
	if(!event) event = window.event;
	if(!event.target)
		event.target = event.srcElement;
	var a = document.createElement("a");
	a.href = event.target.data;
	a.appendChild(document.createTextNode("Download zip " + event.target.data));
	a.target = "_blank";
	dropContainer.appendChild(document.createElement("br"));
	dropContainer.appendChild(a);
}

function mkdirClick()
{
	var req = getXMLHttpRequest();
	var path= prompt("Folder name", "");
	if(!path || !path.length)
		return;
	req.open("MKCOL", "/files/"+list_path+(list_path.length >0?"/":"")+path+ "/", true);
	req.onreadystatechange = function(){
		if (req.readyState === XMLHttpRequest_DONE) {
				var status = req.status;
			writelog(status, true);
			if ((status === 0 || (status >= 200 && status < 400))) {
				updateList();
			}
		}
	}
	req.send("");
}

function moveClick(event)
{
	if(!event) event = window.event;
	if(!event.target)
		event.target = event.srcElement;
	var newName = prompt("New filename", event.target.data);
	if(!newName || !newName.length)
		return;
	if(newName.indexOf("/") < 0)
		newName = "/files/"+list_path+(list_path.length >0?"/":"")+newName;
	var req = getXMLHttpRequest();
	req.open("MOVE", event.target.data, true);
	req.setRequestHeader("Destination", newName);
	req.onreadystatechange = function(){
		if (req.readyState === XMLHttpRequest_DONE) {
				var status = req.status;
			writelog(status, true);
			if ((status === 0 || (status >= 200 && status < 400))) {
				updateList();
			}
		}
	}
	req.send("");
}
var legacyframe = document.getElementById("legacyframe");
var enable_xhr = document.getElementById("enable_xhr");
var enable_fileapi = document.getElementById("enable_fileapi");

function updateList()
{
	if(oldFileList)
		fileListContainer.removeChild(oldFileList);
	oldFileList = null;
	var req = getXMLHttpRequest();
	if(!req)
	{
		enable_xhr.checked = false;
		enable_xhr.disabled = true;
	}
	if(!enable_xhr.checked)
	{
		legacyframe.style.display = "block";
		legacyframe.src = "/index/"+list_path;
		return;
	}
	if(list_path.length)
		pathDisplay.innerText = list_path;
	else
		pathDisplay.innerText = "(root)";
	writelog("Listing files: " + "/list/"+list_path, true);

	try{
		req.open("GET", "/list/"+list_path, true);
	}
	catch(e)
	{
		//enable_xhr.disabled = true;
		enable_xhr.checked = false;
		writeerror(e);
		writelog("XHR disabled",true);
		return;
	}

	req.onreadystatechange = function(){
		if (req.readyState === XMLHttpRequest_DONE) {
				var status = req.status;
			writelog("status("+list_path+"):"+status, true);
			try{

			if ((status === -1 || status === 0 || (status >= 200 && status < 400))) {
				var tbl = document.createElement("table");
				tbl.setAttribute("border", "2");
				var tbd = document.createElement("tbody");
				var row = document.createElement("tr");
				var cell = document.createElement("td");
				cell.width = "100%";
				if(list_path.length)
				{
					cell.appendChild(document.createTextNode(".."))
					cell.onclick = folderUp;
				}
				else
					cell.appendChild(document.createTextNode("(root)"));
				row.appendChild(cell);
				cell = document.createElement("td");
				cell.appendChild(document.createTextNode("<up>"));
				row.appendChild(cell);
				tbd.appendChild(row);

				//var str = "<a  onclick=\"folderUp();\">..</a><br>"
				var arr = parseJSON(req.responseText);
				for(var i = 0; i < arr.length; i++)
				{
					var item = arr[i];
					if(item.type == 1)
					{
						row = document.createElement("tr");
						cell = document.createElement("td");
						link = document.createElement("a");
						link.appendChild(document.createTextNode(item.name));
						link.href="/files/"+list_path+(list_path.length >0?"/":"")+item.name;
						link.target="_blank";
						cell.appendChild(link)
						row.appendChild(cell);
						cell = document.createElement("td");
						cell.appendChild(document.createTextNode(item.size));
						row.appendChild(cell);
						cell = document.createElement("td");
						link = document.createElement("button");
						link.data = "/files/"+list_path+(list_path.length >0?"/":"")+item.name;
						link.onclick = moveClick;
						link.appendChild(document.createTextNode("mv"));
						cell.appendChild(link);
						row.appendChild(cell);
						cell = document.createElement("td");
						link = document.createElement("button");
						link.data = "/files/"+list_path+(list_path.length >0?"/":"")+item.name;
						link.onclick = delClick;
						link.appendChild(document.createTextNode("del"));
						cell.appendChild(link);
						row.appendChild(cell);
						cell = document.createElement("td");
						link = document.createElement("button");
						link.data = "/files/"+list_path+(list_path.length >0?"/":"")+item.name;
						if(item.size < 65536)
							link.onclick = editClick;
						else
							link.disabled = true;
						link.appendChild(document.createTextNode("ed"));
						cell.appendChild(link);
						row.appendChild(cell);
						tbd.appendChild(row);
					}
					//str += "<a href=\"/files/"+list_path+(list_path.length >0?"/":"")+item.name+"\">"+item.name+" ("+item.size+")</a><a onclick=\"delfile('/files/"+list_path+(list_path.length >0?"/":"")+item.name+"')\">del</a><br>";
					else if(item.type == 0)
					{
						row = document.createElement("tr");
						cell = document.createElement("td");
						link = document.createElement("a");
						link.onclick = folderClick;
						link.data = item.name;
						link.appendChild(document.createTextNode(item.name));
						cell.appendChild(link);
						row.appendChild(cell);
						cell = document.createElement("td");
						cell.appendChild(document.createTextNode("<dir>"));
						row.appendChild(cell);
						cell = document.createElement("td");
						link = document.createElement("button");
						link.data = "/files/"+list_path+(list_path.length >0?"/":"")+item.name;
						link.onclick = moveClick;
						link.appendChild(document.createTextNode("mv"));
						cell.appendChild(link);
						row.appendChild(cell);
						cell = document.createElement("td");
						link = document.createElement("button");
						link.data = "/files/"+list_path+(list_path.length >0?"/":"")+item.name;
						link.onclick = delDirClick;
						link.appendChild(document.createTextNode("del"));
						cell.appendChild(link);
						row.appendChild(cell);
						cell = document.createElement("td");
						link = document.createElement("button");
						link.data = "/zip/"+list_path+(list_path.length >0?"/":"")+item.name+".zip";
						link.onclick = zipClick;
						link.appendChild(document.createTextNode("zip"));
						cell.appendChild(link);
						row.appendChild(cell);
						tbd.appendChild(row);
					}
						//str += "<a onclick=\"folderWalk('"+item.name+"');\">"+item.name+"</a><br>";
				}
				tbl.appendChild(tbd);
				//document.getElementById("filelist").innerHTML=str;
				legacyframe.style.display = "none";

				fileListContainer.appendChild(tbl);
				//fileListContainer.parentNode.removeChild(document.getElementById("legacyframe"))
				oldFileList = tbl;
				

			} else {
			}
			}catch(e){
				writeerror(e);legacyframe.style.display = "block";}
		}
	}
	req.send("");
	
}


enable_xhr.checked = true;
enable_xhr.onclick = updateList;

var filesdir = document.getElementById("filesdir")
filesdir.onchange = function(event) {
	if(!event) event = window.event;
	if(!event.target)
		event.target = event.srcElement;
	processFiles(event.target.files);
};

function uploadZip()
{
	if(!enable_fileapi.checked)
	{
		try
		{
			document.getElementById("zipform").submit();
		}
		catch(e){writeerror(e);document.getElementById("uploadzip").onclick = null;}
		return true;
	}

	var req = getXMLHttpRequest();
	req.open("PUT", "/zip/"+list_path+ (list_path.length >0?"/":""), true);
	document.getElementById("uploadzip").disabled = true;
	req.onreadystatechange = function(){
		if (req.readyState === XMLHttpRequest_DONE) {
			document.getElementById("uploadzip").disabled = false;
			var status = req.status;
			writelog("status(zip:"+list_path+"):"+status, true);

			if ((status === -1 || status === 0 || (status >= 200 && status < 400))) {
				alert(req.responseText);
				writelog(req.responseText);
				updateList();
			}
		}
	}
	req.send(document.getElementById("zipfile").files[0]);
	return false;
}

function updateFileAPI()
{
	var uploadzip = document.getElementById("uploadzip");
	if(enable_fileapi.checked)
	{
		filesdir.disabled = false;
		document.getElementById("uploadlegacy").style.display = "none";
		uploadzip.disabled = document.getElementById("zipfile").files && document.getElementById("zipfile").files.length == 0;
		uploadzip.onclick = uploadZip;
		uploadButton.unclick = startUpload;
	}
	else
	{

		filesdir.disabled = true;
		uploadButton.disabled = true;
		document.getElementById("uploadlegacy").style.display = "block";
		uploadzip.disabled = false;
	}
}

try
{
	if("files" in filesdir && filesdir.files !== undefined)
	{
		enable_fileapi.checked = true;
		enable_fileapi.disabled = false;
	}
	else
	{
		enable_fileapi.checked = false;
		enable_fileapi.disabled = true;
		writelog("Your browser does not support FileList API, POST request will be used");
		writelog("Use ZIP files to upload folders", true);
	}
	updateList();
}catch(e){writeerror(e);}
try{
	updateFileAPI();
}catch(e){writeerror(e);}
try{
	enable_fileapi.onclick = updateFileAPI;


	
	document.getElementById("file").onchange = function(event) {
		if(!event) event = window.event;
		if(!event.target)
			event.target = event.srcElement;
		processFiles(event.target.files);
	};
	document.getElementById("zipfile").onchange = function(event) {
		if(enable_fileapi.checked)
			document.getElementById("uploadzip").disabled = document.getElementById("zipfile").files.length == 0;
		else
			document.getElementById("uploadzip").disabled = false;
	};
}catch(e){writeerror(e);}


</script>


</body>
</head>